const Koa = require('koa')
const router = require('koa-router')
const send = require('koa-send')
const app = new Koa()
const pageRouter = require('./routes/dev-ssr')
// const isDev = process.env.NODE_ENV === "development"
const path = require('path')

app.use(async (ctx, next) => {
    if(ctx.path === '/favicon.ico') {
        await send(ctx, '/favicon.ico', {root: path.join(__dirname, '../')})
    }else{
        await next()
    }
})
// koa中间件
app.use(async (ctx, next) => {
    try{
        console.log(ctx.path)
        // next执行下一个中间件
        await next()
    }catch(err) {
        console.log(err, 'err')
        ctx.status = 500
        if(err) {
            ctx.body = err.message
        }
    }
})
// koa-router 的既定用法 .routes是调用路由中间件
app.use(pageRouter.routes()).use(pageRouter.allowedMethods())

const HOST = process.env.NODE_ENV || '192.168.14.78'
const PORT = process.env.PORT || '3333'

app.listen(PORT, HOST, () => {
    console.log(`server is listening on ${HOST}:${PORT}`)
})